﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using BlogSilverlightConsumer.ServiceReference1;

namespace BlogSilverlightConsumer
{
    
    public partial class MainPage : UserControl
    {

        public MainPage()
        {
            InitializeComponent();
            /*
            PostGrid.Columns.Add(new DataGridTextColumn
            {
                Header = "ID",
                Binding = new Binding("Id")
            });

            PostGrid.Columns.Add(new DataGridTextColumn
            {
                Header = "Conteudo",
                Binding = new Binding("Conteudo"),
                Width = new DataGridLength(140)
            });

            PostGrid.Columns.Add(new DataGridTextColumn
            {
                Header = "Autor",
                Binding = new Binding("Autor"),
                Width = new DataGridLength(100)
            });

            PostGrid.Columns.Add(new DataGridTextColumn
            {
                Header = "Categoria",
                Binding = new Binding("Categoria"),
                Width = new DataGridLength(100)
            });
             * */
            LimpaTextBox();
            carregarPostsGrid();
        }
        private void carregarPostsGrid()
        {
            ServiceReference1.Service1SoapClient proxy = new Service1SoapClient();

            proxy.retornarTodosPostsCompleted
                += new EventHandler<retornarTodosPostsCompletedEventArgs>(post_RetornarTodosPosts);

            proxy.retornarTodosPostsAsync();
        }

        void post_RetornarTodosPosts(object sender, retornarTodosPostsCompletedEventArgs e)
        {
            PagedCollectionView pageCollectionView = new PagedCollectionView(e.Result);
            dataPager1.DataContext = pageCollectionView;
            PostGrid.ItemsSource = pageCollectionView;
        }

        private bool Validar()
        {
            if (TxtPostId.Text.Trim().Length == 0)
            {
                MessageBox.Show("O ID não pode estar em branco", "Error", MessageBoxButton.OK);
                TxtPostId.Focus();
                return false;
            }
            else if (TxtPostConteudo.Text.Trim().Length == 0)
            {
                MessageBox.Show("O Conteudo não pode estar em branco", "Error", MessageBoxButton.OK);
                TxtPostConteudo.Focus();
                return false;
            }
            else if (TxtPostAutor.Text.Trim().Length == 0)
            {
                MessageBox.Show("O Autor não pode estar em branco", "Error", MessageBoxButton.OK);
                TxtPostAutor.Focus();
                return false;
            }
            else if (TxtPostCategoria.Text.Trim().Length == 0)
            {
                MessageBox.Show("A Categoria não pode estar em branco", "Error", MessageBoxButton.OK);
                TxtPostCategoria.Focus();
                return false;
            }
            else
            {
                return true;
            }
        }

        private void PostGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.MouseLeftButtonUp += new MouseButtonEventHandler(Row_MouseLeftButtonUp);
        }

        void Row_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            ServiceReference1.Post post = PostGrid.SelectedItem as ServiceReference1.Post;
            TxtPostId.Text = ""+post.Id;
            TxtPostConteudo.Text = post.Conteudo;
            TxtPostAutor.Text = post.Autor;
            TxtPostCategoria.Text = post.Categoria;
        }

        private void UpdateButton_Click(object sender, RoutedEventArgs e)
        {
            if (Validar())
            {
                ServiceReference1.Service1SoapClient proxy = new Service1SoapClient();

                proxy.atualizarPostCompleted += new EventHandler<atualizarPostCompletedEventArgs>
                    (postClient_atualizarPostCompleted);
                int id = Convert.ToInt32(TxtPostId.Text);
                string conteudo = TxtPostConteudo.Text;
                string autor = TxtPostAutor.Text;
                string categ = TxtPostCategoria.Text;
                proxy.atualizarPostAsync(id, conteudo, autor, categ);
            }
        }

        void postClient_atualizarPostCompleted(object sender, atualizarPostCompletedEventArgs e)
        {
            MessageBox.Show(e.Result);
            LimpaTextBox();
            carregarPostsGrid();
        }

        private void LimpaTextBox()
        {
            TxtPostId.Text = string.Empty;
            TxtPostConteudo.Text = string.Empty;
            TxtPostAutor.Text = string.Empty;
            TxtPostCategoria.Text = string.Empty;
            TxtPostId.Focus();
        }

        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            if (TxtPostId.Text == "")
            {
                MessageBox.Show("Selecione um registro para deletar", "Deletar", MessageBoxButton.OK);
            }
            else
            {
                if (MessageBox.Show("Confirma exclusão deste registro ? ", "Deletar", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    ServiceReference1.Service1SoapClient proxy = new Service1SoapClient();

                    proxy.removerPostCompleted += new EventHandler<removerPostCompletedEventArgs>(post_removerPostCompleted);

                    proxy.removerPostAsync(Convert.ToInt32(TxtPostId.Text));
                }
            }
        }

        void post_removerPostCompleted(object sender, removerPostCompletedEventArgs e)
        {
            MessageBox.Show(e.Result);
            LimpaTextBox();
            carregarPostsGrid();
        }

        private void InsertButton_Click(object sender, RoutedEventArgs e)
        {
            if (Validar())
            {
                ServiceReference1.Service1SoapClient proxy = new Service1SoapClient();

                proxy.adicionarPostCompleted += new EventHandler<adicionarPostCompletedEventArgs>
                    (postClient_adicionarPostCompleted);
                int id = Convert.ToInt32(TxtPostId.Text);
                string conteudo = TxtPostConteudo.Text;
                string autor = TxtPostAutor.Text;
                string categ = TxtPostCategoria.Text;
                proxy.adicionarPostAsync(conteudo, autor, categ);
            }
        }

        void postClient_adicionarPostCompleted(object sender, adicionarPostCompletedEventArgs e)
        {
            MessageBox.Show(e.Result);
            LimpaTextBox();
            carregarPostsGrid();
        }

        private void HyperlinkButton_Click_1(object sender, RoutedEventArgs e)
        {
            App.Navigate(new Comentarios());
        }
    }
}
